﻿using Coolzon.UI.Common.RealtimeProxies;
using Coolzon.UI.Core.Views;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Common
{
    public class UIContext : INotifyPropertyChanged
    {
        private static UIContext s_context;
        public static UIContext Current
        {
            get
            {
                if (s_context == null)
                {
                    s_context = new UIContext();
                }
                return s_context;
            }
        }

        #region fields
        
        /// <summary>
        /// 是否自动播放
        /// </summary>
        private bool _isAutoPlay;

        private bool _isUnderControl;

        private bool _isFullScreen;
        
        #endregion

        /// <summary>
        /// 方案名称
        /// loadingView完成之后才会更新
        /// </summary>
        public string SolutionName { get; private set; }
        /// <summary>
        /// 方案宽度
        /// </summary>
        public double SolutionWidth { get; set; }
        /// <summary>
        /// 方案高度
        /// </summary>
        public double SolutionHeight { get; set; }

        /// <summary>
        /// 当前的方案信息
        /// </summary>
        public RT.Solution Solution { get; set; }

        /// <summary>
        /// 方案存放的路径
        /// </summary>
        public string SolutionDirectory { get; set; }

        public string UserAccount { get; set; }
        public string UserName { get; set; }
        /// <summary>
        /// 用户登录之后的标识
        /// </summary>
        public string UserIdentity { get; set; }

        /// <summary>
        /// 控制域编号
        /// </summary>
        public string ControlDomainId { get; set; }

        /// <summary>
        /// 当前操作员id
        /// </summary>
        public string OperatorIdentity { get; set; }

        /// <summary>
        /// 进入域时是否需要同步视图状态，默认是true
        /// 如果是控制端并且值为false那么就打开第一个场景
        /// </summary>
        public bool IsNeesSyncViewStateWhenEnterDomain { get; set; }

        /// <summary>
        /// 根节点的viewcontainer
        /// </summary>
        public ViewContainer RootViewContainer { get; set; }

        /// <summary>
        /// 获取展示视图
        /// </summary>
        public IFrameContainer RenderView { get; set; }

        /// <summary>
        /// 程序启动的路径
        /// </summary>
        public string AppStartupPath
        {
            get;
            set;
        }
        /// <summary>
        /// 程序的版本
        /// </summary>
        public string AppVersion
        {
            get;
            set;
        }

        /// <summary>
        /// 程序的类型(BrowseApp、ConsoleApp、DisplayApp)
        /// </summary>
        public string AppType
        {
            get;
            set;
        }

        /// <summary>
        /// 操作系统信息
        /// </summary>
        public string OSVersion
        {
            get;
            set;
        }

        /// <summary>
        /// .net版本信息
        /// </summary>
        public string DotNetVersion
        {
            get;
            set;
        }

        /// <summary>
        /// 事件的处理模式，根据不同类型的事件处理模式，事件的处理方式也将不一样
        /// 主要分为：直接处理，转送服务器，不予以处理
        /// </summary>
        public EventProcessMode EventProcessMode
        {
            get;
            set;
        }

        /// <summary>
        /// 是否处于控制模式下
        /// </summary>
        public bool IsUnderControl
        {
            get
            {
                return _isUnderControl;
            }
            set
            {
                if (_isUnderControl != value)
                {
                    _isUnderControl = value;
                    RaisePropertyChanged("IsUnderControl");
                }
            }
        }

        /// <summary>
        /// 是否自动循环播放
        /// </summary>
        public bool IsAutoPlay
        {
            get
            {
                return _isAutoPlay;
            }
            set
            {
                if (_isAutoPlay != value)
                {
                    _isAutoPlay = value;
                    RaisePropertyChanged("IsAutoPlay");
                }
            }
        }

        /// <summary>
        /// 是否显示ActiveX组件
        /// </summary>
        public bool IsShowActiveXPart
        { 
            get; 
            set; 
        }

        private UIContext()
        {
            IsUnderControl = true;
            IsAutoPlay = false;
            IsShowActiveXPart = true;
            IsNeesSyncViewStateWhenEnterDomain = true;

            IsSynchronizingViewState = false;
        }

        public void Reset()
        {
            UserAccount = string.Empty;
            UserIdentity = string.Empty;
            ControlDomainId = string.Empty;
            Solution = null;
            SolutionName = string.Empty;
        }

        /// <summary>
        /// 是否自动创建域
        /// </summary>
        public bool IsAutoCreateDomain
        {
            get;
            set;
        }

        /// <summary>
        /// 是否处于全屏显示状态
        /// </summary>
        public bool IsFullScreen
        {
            get
            {
                return _isFullScreen;
            }
            set
            {
                if (_isFullScreen != value)
                {
                    _isFullScreen = value;
                    RaisePropertyChanged("IsFullScreen");
                }
            }
        }

        /// <summary>
        /// 是否是控制端
        /// </summary>
        /// <returns>true:是控制端，false：不是控制端</returns>
        public bool IsConsoleClient()
        {
            if (this.AppType.ToUpper() == "CONSOLEAPP")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断是否域的操作员
        /// </summary>
        /// <returns></returns>
        public bool IsDomainOperator()
        {
            if (!string.IsNullOrEmpty(this.OperatorIdentity)
                && this.UserIdentity == this.OperatorIdentity)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// true：进入控制域是因为终端创建了此域，不需要同步视图状态需要直接发送打开默认场景的指令，
        /// false：域已存在直接加入进来的，需要同步视图状态，不能发送打开默认场景的指令
        /// </summary>
        public bool IsEnterDomainByCreate
        {
            get;
            set;
        }

        public bool IsSynchronizingViewState { get; set; }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        /// <summary>
        /// 是否正在加载方案
        /// </summary>
        public bool IsLoadingSolution { get; set; }

        /// <summary>
        /// 主服务器地址
        /// </summary>
        public string MainServerIP { get; private set; }

        /// <summary>
        /// 主服务端口
        /// </summary>
        public int MainServerPort { get; private set; }

        /// <summary>
        /// 正在被切换为主服务的服务器信息，
        /// 如果这个值不为空代表切换主服务线程发送了切换指令，但是服务器还没有返回结果。
        /// </summary>
        public Endpoint MainServer4Switching { get; private set; }
        /// <summary>
        /// 记录打开过的场景
        /// </summary>
        public Stack<string> OpenedSceneList { get; set; }

        /// <summary>
        /// 设置主服务信息
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public void UpdateMainServer(string ip, int port)
        {
            this.MainServerIP = ip;
            this.MainServerPort = port;
        }

        public void SetSolutionName(string solutionname)
        {
            this.SolutionName = solutionname;
        }

        /// <summary>
        /// 更新正在切换的主服务器信息
        /// </summary>
        /// <param name="server">正在被切换为主服务器的信息</param>
        public void UpdateMainServer4Switching(Endpoint server)
        {
            this.MainServer4Switching = server;
        }
    }
}
